
package com.trial.area.coupon;

import java.util.Random;
import java.util.UUID;

import org.apache.commons.lang3.StringUtils;

/**
 * 券码工具类
 *
 * @author 朱国军
 * @since 1.0
 * @version 2016年8月3日 朱国军
 */
public class CouponUtils {

    /**
     * 根据券码前缀生成券码编号
     *
     * @param pre 券码前缀
     * @return
     */
    public static String createCouponNo(String pre) {
        String uuid = StringUtils.replace(UUID.randomUUID().toString(), "-", "");
        Random random = new Random();
        Integer num = random.nextInt(999999);
        String randomStr = String.valueOf(num);
        if (randomStr.length() < 6) {
            int ramdonStrLength = randomStr.length();
            for (int i = 0; i < (6 - ramdonStrLength); i++) {
                randomStr = "0" + randomStr;
            }
        }

        String couponNo = pre + uuid.substring(3, 5) + "-" + uuid.substring(8, 12) + "-" + randomStr;

        return couponNo.toUpperCase();
    }
}
